{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "74b5f83c-d64b-40a4-906d-57c82fb22679",
   "metadata": {},
   "source": [
    "# 第三节课读书笔记"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a623617f-5567-4803-abb6-b9e4a438734d",
   "metadata": {},
   "source": [
    "## Python中变量的命名\n",
    "\n",
    "Python中变量的名字:数字+字母+“_”三类组成；首字母不能是数字；不能与“保留字”相同。\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ba935530-bb7f-40a6-8497-f9d27079dfd6",
   "metadata": {},
   "source": [
    "1. 尽可能不适用i,j等无意义变量名\n",
    "2. 尽可能使用store等有意义名称\n",
    "3. 多个单词组成的变量使用“_”连接\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "90d87c4a-6a04-4aea-bc32-9d6acbbf8c64",
   "metadata": {},
   "source": [
    "* 尽可能不适用i,j等无意义变量名\n",
    "* 尽可能使用store等有意义名称\n",
    "* 多个单词组成的变量使用“_”连接\n",
    "\n",
    "程序代码示例\n",
    "\n",
    "        i=1\n",
    "        print(i)\n",
    "        \n",
    "***"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7f0e1e55-d118-4e18-b436-5e1522489803",
   "metadata": {},
   "source": [
    "## Python变量的类型"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a5385aaa-6d68-4b6c-a63e-d219feb3acf8",
   "metadata": {},
   "source": [
    "Python变量的类型分为三类：\n",
    "* 数值型：int,float,complex,bool\n",
    "* 字符型:str\n",
    "* 空:None"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "4bfce910-74c8-4704-a453-4eff42d53627",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'int'>\n",
      "<class 'float'>\n"
     ]
    }
   ],
   "source": [
    "x=1\n",
    "print(type(x))\n",
    "x=1.\n",
    "print(type(x))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "5fbe577a-7052-4181-83df-601f870b3600",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "isinstance(x,float)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "500a9aac-503a-41d6-8979-03297a9b9069",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1 1\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "False"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a='1'\n",
    "b=1\n",
    "print(a,b)\n",
    "a==b"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6a856eb7-76ca-424b-a56e-76dbaf54bbc9",
   "metadata": {},
   "source": [
    "## Python的运算符"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8961f6cf-1eb8-4cec-a860-bd41fd32883a",
   "metadata": {},
   "source": [
    "- 数值计算: + ，-， *，/， ** ，%，//，+=，-=，/=，==，<，>，<=，>=，&，|，^，!，()\n",
    "- 字符串运算符：+  *"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "bf7d481a-2d77-42d9-b7a0-16f1f61dcb76",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1\n",
      "3\n",
      "3\n"
     ]
    }
   ],
   "source": [
    "print(10%3)  #取余数\n",
    "print(10//3)  #取得数的整数\n",
    "\n",
    "a=1\n",
    "a+=2\n",
    "print(a)\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "c84e61e4-0c09-4e0f-b1a7-508547676d25",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0\n",
      "4\n"
     ]
    }
   ],
   "source": [
    "a=3&4   #取两数二进制的“与”（或门和与门） （0011 0100）\n",
    "print(a)\n",
    "a=6&4   #0110 0100\n",
    "print(a)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "5349060c-4370-4347-b2bd-bc8a39f80cc6",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "hahhhahh hahhlall\n"
     ]
    }
   ],
   "source": [
    "a=\"hahh\"\n",
    "b=\"lall\"\n",
    "print(a*2,a+b)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "fad4c7cc-c36b-4d2f-ad60-cc5772f20810",
   "metadata": {},
   "source": [
    "## Python变量的扩展"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "3ec16126-2412-430a-a138-9464d50afe7b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a=True\n",
    "b=0\n",
    "a+b"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "73f86ed0-e584-41e1-a905-fa0a04423fdd",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "True==1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "690aeb04-897b-42b6-bef7-97be83c6c029",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "False==0"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0e70bc7e-c303-4c46-84e6-30e1508bcdc8",
   "metadata": {},
   "source": [
    "## Python的字符串函数与切片"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "b2e68f2e-725f-4294-ace7-78e83b852d4f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "4"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a=\"hahh\"\n",
    "len(a)   #字符串的长度"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "id": "8fa015a1-a707-449f-994a-58a20ad36649",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'int'>\n"
     ]
    }
   ],
   "source": [
    "a=\"1211\"\n",
    "b=eval(a)   #把字符串改为数字\n",
    "print(type(b))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "id": "101b53ac-7c29-4e74-a199-caff2df9778e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "\"ha\" in a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "id": "2e47d4ba-459f-4556-9741-291deaaa2f81",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'ho'"
      ]
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a=\"guangzhou\"\n",
    "a[6:8]  #字符串切片[start，end，step]，左闭右开"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "id": "9f6ebef7-d0d1-405f-9817-d48ca5a6d0d4",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'22 37.0'"
      ]
     },
     "execution_count": 41,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a=\"10\t \t*\t00 22 37.0\t2\t+63 52.0\t2\t110.0\t17.0\t \t5.4\t00 25 25.1\t+64 08 36\"\n",
    "a[10:17]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "85867358-1ab2-494f-bab8-2c62a0fba373",
   "metadata": {},
   "source": [
    "## python的格式化输出\n",
    "\n",
    "格式化输出的三种方法\n",
    "- %\n",
    "- format\n",
    "- fstring"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 54,
   "id": "571ad0eb-08fc-404f-aa8a-83c925a69c67",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "   3   4\n",
      "0003   4\n",
      "003.1416\n"
     ]
    }
   ],
   "source": [
    "a=3\n",
    "b=4\n",
    "pi=3.1415926535\n",
    "print(\"%4d%4d\" % (a,b))\n",
    "print(\"%04d%4d\" % (a,b))\n",
    "print(\"%08.4f\" % pi)   #\"0\"指用填充，\"8\"指占八个字符，.后指小数点后保留几位数"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "561d055b-7e6c-4a0e-82ea-68bb0fd7ae3d",
   "metadata": {},
   "source": [
    "# 作业1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 73,
   "id": "6ea4970b-e7c5-4ffc-935d-6fc7b82c8433",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "  77.699  11.692   0.570  55.864  20.420  67.741  45.462  60.280  93.121  78.001\n",
      "  76.881  51.072  52.070  60.465  78.337   5.228  11.107   2.359  36.793  49.985\n",
      "  75.147  54.764  61.594  63.443  91.350  29.743  69.720  46.715  15.785  18.364\n",
      "  44.329  12.132  77.275  37.797  66.417  74.474  20.291  22.775  10.569  24.943\n",
      "  88.800  85.106  88.999  76.640   1.869  12.416  20.395  92.915  13.780  79.826\n",
      "  94.636  36.533  53.705  87.463  87.421  56.348  74.103  71.354  34.267  57.796\n",
      "   9.962  99.763  74.983  67.652  70.830  76.417  15.793  16.837  57.444  45.783\n",
      "  54.710  84.167  55.328  26.968  82.125  91.655   7.057  52.777  36.591  53.928\n",
      "  60.195   0.071  76.483  77.384  96.415  71.256  45.865  35.862  73.149  58.390\n",
      "   9.925  92.134   6.051  41.274  69.247  98.815   3.741  12.098  84.836  22.700\n"
     ]
    }
   ],
   "source": [
    "import random\n",
    "i=0\n",
    "for i in range(0,100):\n",
    "    a=random.uniform(0,100)\n",
    "    b=(\"%8.3f\" % a)\n",
    "    print(b,end=\"\")\n",
    "    if (i+1)%10==0:\n",
    "        print()\n",
    "    i+=1  "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "18a1b533-c8cb-466b-ab91-2c30bd49650a",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
